package com.xxx.blog.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.blog.domain.ArticleType;
import com.xxx.blog.query.ArticleTypeQuery;
import com.xxx.blog.service.IArticleTypeService;

import com.xxx.blog.vo.ArticleTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articleType")
@Api(tags = "文章类型接口类")
public class ArticleTypeController {

    @Autowired
    private IArticleTypeService articleTypeService;

    @GetMapping
    @ApiOperation(value = "查询所有")
    public R<List<ArticleType>> findAll() {
        List<ArticleType> ArticleTypes = articleTypeService.findAll();
        return R.ok(ArticleTypes);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public R<ArticleType> findById(@PathVariable long id) {
        ArticleType ArticleType = articleTypeService.findById(id);
        return R.ok(ArticleType);
    }

    @PostMapping
    @ApiOperation(value = "分页条件查询")
    public  PageInfo<ArticleType> queryPage(@RequestBody ArticleTypeQuery ArticleTypeQuery) {
        List<ArticleType> list = articleTypeService.queryPage(ArticleTypeQuery);
        return new PageInfo<ArticleType>(list);
    }

    @PutMapping
    @ApiOperation(value = "增加或者修改")
    public R<Void> addOrUpdateArticleType(@RequestBody ArticleType ArticleType) {
        try {
            Long id = ArticleType.getId();
            if (id == null) {
                articleTypeService.addArticleType(ArticleType);
            } else {
                articleTypeService.updateArticleType(ArticleType);
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.fail("文章类型添加失败");
        }
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除一个文章类型")
    public R<Void> deleteArticleType(@PathVariable Long id) {
        try {
            articleTypeService.deleteArticleType(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @PatchMapping
    @ApiOperation(value = "批量删除文章类型")
    public R<Void> batchArticleType(@RequestBody Long[] ids) {
        try {
            articleTypeService.batchArticleType(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("已经存在");
        }
        return R.ok();
    }

    @GetMapping("/getTypeTree/{id}")
    @ApiOperation(value = "查询所有类型树")
    public R<List<ArticleType>> getTypeTree(@PathVariable Long id) {
        List<ArticleType> ArticleTypes = articleTypeService.getTypeTree(id);
        return R.ok(ArticleTypes);
    }

    @GetMapping("/articleTypeStatistics")
    @ApiOperation(value = "查询前端文章分类")
    public List<ArticleTypeVo> getArticleTypeStatistics() {
        List<ArticleTypeVo> list  = articleTypeService.getArticleTypeStatistics();
        return list;
    }

}
